#include<bits/stdc++.h>
using namespace std;
using ll = long long;
string s[22];
ll n;
int ans = 0;
int vis[22];
void dfs(const string & tmp)
{
    ans = max(ans, (int)tmp.size());
    for (int i = 1; i <= n;i++)
    {
        if(vis[i]>=2)
            continue;
        else {
            for (ll j = 1; j < min(s[i].size(),tmp.size());j++)
            {
                if(tmp.substr(tmp.size()-j) == s[i].substr(0,j))
                {
                    vis[i]++;
                    dfs(tmp + s[i].substr(j));
                    vis[i]--;
                }
            }
        }
    }
}
int main()
{
    cin >> n;
    for (ll i = 1; i <= n;i++)
    {
        cin >> s[i];
    }
    char c;
    cin >>  c;
    for (ll i = 1; i <= n;i++)
    {
        if(s[i][0]==c)
        {
            vis[i]++;
            dfs(s[i]);
            vis[i]--;
        }
    }
    cout << ans << endl;
}